package uit.uthuoc.activity;

import java.net.URI;
import java.util.ArrayList;

import uit.uthuoc.dal.DrugDAO;
import uit.uthuoc.dal.MIMSClassDTO;
import uit.uthuoc.fragment.DrugSearchFragment;
import uit.uthuoc.fragment.SearchResultFragmentInterface;

import com.uit.uthuoc.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;



public class DrugSearchActivity extends SingleFragmentActivity implements SearchResultFragmentInterface{
	
	private static final String LOG_ID = "uit.uThuoc.activity.DrugSearchActivity";
	public static final String EXTRA_MIMS = "uit.uThuoc.activity.DrugSearchActivity.EXTRA_MIMS";
	
	DrugSearchFragment mChildFragment;
	
	@Override
	protected Fragment createFragment() {
		return mChildFragment;
	}
	
	@Override
	protected boolean isUpButtonEnable() {
		// TODO Auto-generated method stub
		return true;
	}
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	MIMSClassDTO mims = null;
        if (getIntent().getData() != null) 
        {
        	Log.d(LOG_ID, "A link is invoked, data is: " + getIntent().getDataString());
        	int mimsClassId = Integer.parseInt(getIntent().getData().getPathSegments().get(0));
//        	mimsClassName = uit.uthuoc.utils.StringUtils.unAccent(mimsClassName);
        	Log.d(LOG_ID, "Path is: " + mimsClassId);
        	
        	// Check if result's size is zero
        	mims = uit.uthuoc.dal.DrugDAO.getDAO().searchMIMSClassByMIMSClassID(mimsClassId);
        	Log.d(LOG_ID, mims.toString());
        } else {
    	
	        mims = (MIMSClassDTO) getIntent().getSerializableExtra(EXTRA_MIMS);
	        Log.d(LOG_ID, "Extra mims is: " + mims);
       
        }
        if (mims == null) {
    		mChildFragment = new DrugSearchFragment();
            Log.d(LOG_ID, "onCreate called, fragment instance was created.");
        }
        else {
        	mChildFragment = DrugSearchFragment.newInstance(mims);
        }
        super.onCreate(savedInstanceState);
        
        
    }
    



	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
//        
//        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//        MenuItem searchItem = menu.findItem(R.id.action_search);
//        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//        
//        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//        searchView.setIconifiedByDefault(false);
        
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	public void onListItemClickListener(int drugId) {
		Log.d(LOG_ID, "Drug " + drugId + " was clicked");
		Intent intent = new Intent(this, DrugDetailsActivity.class);
		intent.putExtra(DrugDetailsActivity.EXTRA_INTENT_INT_ID, drugId);
		startActivity(intent);
	}
}
